package com.hdoit.kylin.web.door.controllers;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.aliyun.oss.common.utils.DateUtil;
import com.hdoit.kylin.logical.baseInfo.model.Term;
import com.hdoit.kylin.logical.baseInfo.service.ITermService;
import com.hdoit.kylin.logical.document.model.WeekLog;
import com.hdoit.kylin.logical.document.service.IWeekLogService;
import com.hdoit.kylin.logical.groupActivity.model.AcademicReport;
import com.hdoit.kylin.logical.groupActivity.model.GroupDuty;
import com.hdoit.kylin.logical.groupActivity.service.IAcademicReportService;
import com.hdoit.kylin.logical.groupActivity.service.IGroupDutyService;
import com.hdoit.kylin.logical.user.model.Student;
import com.hdoit.kylin.logical.user.model.Teacher;
import com.hdoit.kylin.logical.user.service.IStudentService;
import com.hdoit.kylin.logical.user.service.ITeacherService;
import com.hdoit.kylin.logical.viewmodel.document.WeekLogSearch;
import com.hdoit.kylin.logical.viewmodel.groupActivity.GroupDutySearch;
import com.hdoit.kylin.logical.viewmodel.user.TeacherSearch;
import com.hdoit.kylin.web.common.CommonWebUtils;
import com.tennetcn.common.aop.CommonApplicationContextUtil;
import com.tennetcn.common.enums.ManageContainer;
import com.tennetcn.common.enums.pub.YesOrNoInteger;
import com.tennetcn.common.logical.superbase.model.Employee;
import com.tennetcn.common.logical.superbase.service.IEmployeeService;
import com.tennetcn.common.message.PagerModel;
import com.tennetcn.common.utils.DateUtils;
import com.tennetcn.component.web.BaseController;

/**
 * 
 * @author ridfer
 *
 */

@Controller
@RequestMapping(value="/admin/information/")
public class InformationController extends BaseController {

	@Override
	public String getRoutePath() {
		return "areas/admin/information/";
	}
	
	private IEmployeeService employeeService;
	private IGroupDutyService groupDutyService;
	private IWeekLogService weeklogService;	
	private ITeacherService teacherService;
	private IStudentService studentService;
	
	public ITeacherService getTeacherService() {
		return teacherService;
	}

	@Resource
	public void setTeacherService(ITeacherService teacherService) {
		this.teacherService = teacherService;
	}

	public IStudentService getStudentService() {
		return studentService;
	}

	@Resource
	public void setStudentService(IStudentService studentService) {
		this.studentService = studentService;
	}

	public IEmployeeService getEmployeeService() {
		return employeeService;
	}

	@Resource
	public void setEmployeeService(IEmployeeService employeeService) {
		this.employeeService = employeeService;
	}

	public IGroupDutyService getGroupDutyService() {
		return groupDutyService;
	}

	@Resource
	public void setGroupDutyService(IGroupDutyService groupDutyService) {
		this.groupDutyService = groupDutyService;
	}

	public IWeekLogService getWeeklogService() {
		return weeklogService;
	}

	@Resource
	public void setWeeklogService(IWeekLogService weeklogService) {
		this.weeklogService = weeklogService;
	}

	@RequestMapping(value="main",method={RequestMethod.GET,RequestMethod.POST})
	public String main(Model model, PagerModel pagerModel,boolean ajaxPost){
		System.err.println("admin/main");
		String groupId;
		if(CommonWebUtils.isTeacher(getLoginModel())){
			Teacher teacher = teacherService.queryModel(getLoginModel().getLoginId());
			groupId = teacher.getGroupId();
			model.addAttribute("loginUser",teacher);
		}
		else if(CommonWebUtils.isStudent(getLoginModel())){
			Student student = studentService.queryModel(getLoginModel().getLoginId());
			groupId = student.getGroupId();
			model.addAttribute("loginUser",student);
		}
		else{
			return view("blank");
		}
		Date currentDate = DateUtils.getCurrent();
		Calendar calendar = Calendar.getInstance();
		int weekLogStatus;
		{
			WeekLogSearch search = new WeekLogSearch();
			search.setEmployeeId(getLoginModel().getLoginId());
			List<WeekLog> weekLogs = weeklogService.queryList(search);
			if(weekLogs.size()>0){
				Date weeklogDate = weekLogs.get(0).getCreateDate();
				calendar.setTime(weeklogDate);
				calendar.add(calendar.DATE, 8-calendar.get(calendar.DAY_OF_WEEK));
				weeklogDate = calendar.getTime();
				if(currentDate.before(weeklogDate)){
					weekLogStatus=YesOrNoInteger.yes;
				}
				else {
					weekLogStatus = YesOrNoInteger.no;
				}
			}
			else{
				weekLogStatus = YesOrNoInteger.no;
			}
		}
		AcademicReport academicReport = new AcademicReport();
		int reportStatus = YesOrNoInteger.no;
		
		{
			IAcademicReportService academicReportService = CommonApplicationContextUtil.getCurrentContext().getBean(IAcademicReportService.class);
			List<AcademicReport> academicReports = academicReportService.queryList();
			if(academicReports.size()>0){
				for(AcademicReport temp:academicReports){
					if(temp.getDate().after(currentDate)){
						academicReport = temp;
						reportStatus = YesOrNoInteger.yes;
					}else{
						break;
					}
				}
			}
			
			
		}
		
		model.addAttribute(ManageContainer.PAGERKEY,pagerModel);
		model.addAttribute("weekLogStatus",weekLogStatus);
		model.addAttribute("reportStatus",reportStatus);
		model.addAttribute("academicReport",academicReport);
		
		return view("main");
	}
	
	

}
